package com.fuershiye.woman.monitor.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.fuershiye.woman.monitor.beans.Result;
import com.fuershiye.woman.monitor.constants.Massage;
import com.fuershiye.woman.monitor.service.*;
import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fuershiye.woman.monitor.beans.IdNamePid;
import com.fuershiye.woman.monitor.beans.IdNameSname;
import com.fuershiye.woman.monitor.beans.ViewForms;
import com.fuershiye.woman.monitor.model.Content;
import com.fuershiye.woman.monitor.model.Forms;
import com.fuershiye.woman.monitor.utils.DateFormatUtil;
import com.fuershiye.woman.monitor.utils.MyUtilString;
import com.fuershiye.woman.monitor.utils.ObjectUtil;
import com.google.common.collect.Lists;

/**
 * 报表的设置
 * 
 * @author yongyongdong
 *
 */
@Controller
@RequestMapping(value = "/forms")
public class FormsController {

	private static final Logger logger = LoggerFactory
			.getLogger(FormsController.class);

	@Autowired
	FormsService formsService;
	@Autowired
	ContentService contentService;
	@Autowired
	IndicatorService indicatorService;
	@Autowired
	DepartmentService departmentService;
	@Autowired
	AreaService areaService;

//	@RequiresPermissions("forms:view")
	@RequestMapping(value = "/view.do")
	public String view(String show, Model model) {

//		List<IdNameSname> types = departmentService.getAllSnames(null,null,null);
//		model.addAttribute("departmentNames", types);
//
//		List<IdNamePid> names = indicatorService.getAllNames();
//		model.addAttribute("indicatorNames", names);

//		List<Integer> years = DateFormatUtil.getAreaYears();
//		model.addAttribute("years", years);

//		List<IdNamePid> areaList = areaService.getAllNames();
//		model.addAttribute("areaList", areaList);

		return "forms/" + show;
	}

//	@RequiresPermissions("forms:list")
	@RequestMapping(value = "/list.do")
	@ResponseBody
	public List<ViewForms> getPage(String name, Integer page, Integer pageSize) {
		
		List<ViewForms> result = Lists.newArrayList();
		
		List<Forms> resultList = formsService.getPage(name, page, pageSize);
		for(Forms forms:resultList){
			String info = null;
			if(forms!=null && forms.getRemark()!=null){
				Content content = contentService.get(forms.getRemark());
				if(content!=null){
					info=content.getInfo();
				}
			}
			result.add(ObjectUtil.turnForms2View(forms, info));
		}
		return result;
	}

//	@RequiresPermissions("forms:save")
	@RequestMapping(value = "/save.do")
	@ResponseBody
	public Result save(Long id, HttpServletRequest request) {
		Forms forms = null;
		boolean first = false;
		if (id == null || id == 0) {
			forms = new Forms();
			first = true;
		} else {
			forms = formsService.get(id);
			if (forms == null) {
				logger.error("FormsController update,FormsController is null");
				return new Result(Massage.FORMS_IS_NULL);
			}
		}

		Forms saveForms = getValueFromPost(first, forms, request);
		formsService.save(saveForms);

		return new Result(Massage.SUCCESS);
	}

//	@RequiresPermissions("forms:delete")
	@RequestMapping(value = "/delete.do")
	@ResponseBody
	public boolean delete(Long id) {
		Forms forms = formsService.get(id);
		forms.setStatus(0);// 修改为已删除
		formsService.save(forms);
		return true;
	}

	private Forms getValueFromPost(boolean first, Forms forms,
			HttpServletRequest request) {

		String name = request.getParameter("name");
		String number = request.getParameter("number");
		String making = request.getParameter("making");
		String code = request.getParameter("code");
		String expire = request.getParameter("expire");
		String remark = request.getParameter("remark");

		if (!MyUtilString.isBlankNullZero(name)) {
			forms.setName(name);
		}
		if (!MyUtilString.isBlankNullZero(number)) {
			forms.setNumber(number);
		}
		forms.setMaking(making);
		if (!MyUtilString.isBlankNullZero(code)) {
			forms.setCode(code);
		}
		if (!MyUtilString.isBlankNullZero(expire)) {
			expire = expire.replace("-", "");
			forms.setExpire(Integer.valueOf(expire));
		}

		if(!MyUtilString.isBlankNull(remark)){
			//未加过备注
			if(forms.getRemark()==null){
				Content content = new Content();
				content.setCtime(new Date());
				content.setInfo(remark);
				content.setMtime(new Date());
				content.setStatus(0);
				content.setType(2);
				contentService.save(content);
				forms.setRemark(content.getId());
			}else{
				Content content = contentService.get(forms.getRemark());
				if(content!=null){
					content.setMtime(new Date());
					content.setInfo(remark);
					contentService.save(content);
				}else{
					logger.error("Error in FormsController getValueFromPost content is null");
				}
			}
		}
		forms.setEditor(SecurityUtils.getSubject().getPrincipal()+"");
		forms.setMtime(new Date());
		forms.setStatus(1);

		return forms;
	}

	//	@RequiresPermissions("forms:delete")
	@RequestMapping(value = "/query/header.do")
	@ResponseBody
	public Result queryHeader(Long id) {

		if(id==null||id==0){
			return new Result(Massage.ERROR_PARAMETER);
		}

		Forms forms = formsService.get(id);
		if(forms==null){
			return new Result(Massage.FORMS_IS_NULL);
		}

		return new Result(Massage.SUCCESS, ObjectUtil.turnForms2ViewFormsHeader(forms));
	}

}
